package com.yehuo.app.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yehuo.app.entity.User;
import com.yehuo.app.entity.UserPassword;
import com.yehuo.app.entity.Wallet;
import com.yehuo.app.mapper.*;
import com.yehuo.app.model.APP;
import com.yehuo.app.service.UserPasswordService;
import com.yehuo.app.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class UserPasswordServiceImpl implements UserPasswordService {
    @Autowired
    UserPasswordMapper userPasswordMapper;
    @Autowired
    private UserMapper userMapper;

    public APP getUserPassword(String userId) {
        QueryWrapper<UserPassword> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        UserPassword password = userPasswordMapper.selectOne(queryWrapper);
        if (password == null) {
            return APP.fail("用户未设置密码",null);
        }
        return APP.success("获取支付配置成功", password);
    }

    public APP checkPassword(UserPassword userPassword) {
        QueryWrapper<UserPassword> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userPassword.getUserId());
        queryWrapper.eq("password", userPassword.getPassword());
        UserPassword userPassword1 = userPasswordMapper.selectOne(queryWrapper);
        if (userPassword1 == null) {
            return APP.fail("用户校验密码不成功",null);
        }
        return APP.success("校验密码成功", userPassword1);
    }
    @Override
    public APP updatePassword(UserPassword userPassword) {
        //判断userId 是否成功
        if (userPassword == null) {
            return APP.fail("参数错误，请对接好参数值", null);
        } else if (StringUtils.isEmpty(userPassword.getUserId())) {
            return APP.fail("参数有误:userId:" + userPassword.getUserId(), null);
        } else {
            QueryWrapper<User> queryWrapper = new QueryWrapper();
            queryWrapper.eq("_uid", userPassword.getUserId());
            User user = this.userMapper.selectOne(queryWrapper);
            if (user == null) {
                return APP.fail("参数有误:userId:" + userPassword.getUserId() + ",该用户不存在", null);
            } else {
                if(StringUtils.isEmpty(userPassword.getPassword()) || userPassword.getPassword().length() != 6){
                    return APP.fail("请输入完整6位数密码",null);
                }
                QueryWrapper<UserPassword> userPasswordQueryWrapper = new QueryWrapper();
                userPasswordQueryWrapper.eq("user_id", userPassword.getUserId());
                UserPassword password = userPasswordMapper.selectOne(userPasswordQueryWrapper);
                if (password == null) {
                    //添加
                    userPassword.setCreateTime(new Date());
                    userPassword.setUpdateTime(userPassword.getCreateTime());
                    userPasswordMapper.insert(userPassword);
                }else{
                    userPassword.setId(password.getId());
                    userPassword.setUpdateTime(password.getCreateTime());
                    userPasswordMapper.updateById(userPassword);
                }
                return APP.success("支付配置成功",userPassword);
            }
        }
    }
}
